﻿using Engine;
using Engine.Graphics;

namespace Game
{
    public class TSwordBlock : Block
    {
        public const int Index = 353;

        private BlockMesh m_mesh = new BlockMesh();

        private BlockMesh m_standaloneMesh = new BlockMesh();

        private BoundingBox[] m_collisionBoxes;

        public Texture2D texture;

        public override void Initialize()
        {
            texture = ContentManager.Get<Texture2D>("Textures/items/tj");
            Model model = ContentManager.Get<Model>("Models/sakuraswords");
            Matrix boneAbsoluteTransform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh("yhj").ParentBone);
            Color white = Color.White;
            BlockMesh blockMesh = new BlockMesh();
            blockMesh.AppendModelMeshPart(model.FindMesh("yhj").MeshParts[0], boneAbsoluteTransform * Matrix.CreateTranslation(0f, 0f, 0f), makeEmissive: false, flipWindingOrder: false, doubleSided: false, flipNormals: false, Color.White);
            m_mesh.AppendBlockMesh(blockMesh);
            m_standaloneMesh.AppendBlockMesh(blockMesh);
            m_collisionBoxes = new BoundingBox[1] { blockMesh.CalculateBoundingBox() };
            base.Initialize();
        }

        public override BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
        {
            return m_collisionBoxes;
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            generator.GenerateShadedMeshVertices(this, x, y, z, m_standaloneMesh, Color.White, null, null, geometry.GetGeometry(texture).OpaqueSubsetsByFace[0]);
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneMesh, texture, color, 2f * size, ref matrix, environmentData);
        }
    }
}